package ch.yoursource.StartPageBuilder;

import java.awt.Component;
import java.awt.Desktop;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

import javax.swing.JFileChooser;
import javax.swing.JPanel;

/**
 * Provides useful functions which can be accessed in a static way
 * @author Marcel Burkhard
 */
public class Toolbox {
    public static final String NEWLINE = System.getProperty("line.separator");
    /**
     * Reads a text file and returns it as a string array where each line is an object
     * @param fileName - file path
     * @return file content as String[]
     */
    public static String[] getFileContents(String fileName) {
        try {
            File file = new File(fileName);
            Scanner fScanner;
            int lines = 0;
            fScanner = new Scanner(new FileInputStream(file));
            for(; fScanner.hasNextLine();fScanner.nextLine()){
                lines++;
            }
            String[] stringArray = new String[lines];
            fScanner = new Scanner(new FileInputStream(file));
            for(int i = 0; fScanner.hasNextLine();i++) {
                stringArray[i] = fScanner.nextLine();
            }
            fScanner.close();
            return stringArray;
        } catch (FileNotFoundException fnfEx) {
            fnfEx.printStackTrace();
            return new String[]{"Error"};
        }
    }
    /**
     * Reads a text file and returns it as a string
     * @param fileName - file path
     * @return file content
     */
    public static String readFile(String fileName) {
        StringBuilder fileContent = new StringBuilder();
        try {
            File file = new File(fileName);
            Scanner fileScanner;
            fileScanner = new Scanner(new FileInputStream(file));
            while(fileScanner.hasNextLine()) {
                fileContent.append(fileScanner.nextLine()+Toolbox.NEWLINE);
            }
        } catch (FileNotFoundException fnfEx) {
            fnfEx.printStackTrace();
        }
        return fileContent.toString();
    }
    /**
     * Reads a text file and returns it as a string
     * @param file - File object
     * @return file content
     */
    public static String readFile(File file) {
        StringBuilder fileContent = new StringBuilder();
        Scanner fileScanner;
        try {
            fileScanner = new Scanner(new FileInputStream(file));
            while(fileScanner.hasNextLine()) {
                fileContent.append(fileScanner.nextLine()+Toolbox.NEWLINE);
            }
        } catch (FileNotFoundException fnfEx) {
            fnfEx.printStackTrace();
        }
        return fileContent.toString();
    }
    /**
     * Writes a string to a file
     * @param text - file content
     * @param fileName - file path
     */
    public static void stringToFile(String text, String fileName) {
        try {
            FileWriter outFile = new FileWriter(fileName);
            outFile.append(text);
            outFile.close();
        } catch (IOException ioEx) {
            ioEx.printStackTrace();
        }
    }
    /**
     * Prints content of text file to console
     * @param fileName - file path
     * @see #readFile(String fileName)
     */
    public static void printFileContents(String fileName) {
        System.out.println(Toolbox.readFile(fileName));
    }
    /**
     * Adds multiple GUI components to a JPanel
     * @param panel - panel which will contain the components
     * @param components - components to add to the panel
     */
    public static void addComponents(JPanel panel, Component... components) {
        for(Component swingObject : components) {
            panel.add(swingObject);
        }
    }
    public static void openFileWithDefaultApplication(String path) {
        Desktop desktop = Desktop.getDesktop();
        try {
            desktop.open(new File(path));
        } catch (IOException e) {
            javax.swing.JOptionPane.showMessageDialog(null,"couldn't open exported file\n"+e.getMessage());
        }
    }
    /**
     * Creates a temporary file containing the content submitted to the function and returns the path
     * @param content - csv data
     * @param suffix - file extension
     * @return path to temp file
     */
    public static String createTempFile(String content, String suffix) {
        try {
            File temp = File.createTempFile("EasyQueries_Export_", suffix);
            temp.deleteOnExit();
            BufferedWriter out = new BufferedWriter(new FileWriter(temp));
            out.write(content);
            out.close();
            return temp.getAbsolutePath();
        } catch (IOException e) {
            javax.swing.JOptionPane.showMessageDialog(null,"couldn't create temporary file\n"+e.getMessage());
            return null;
        }
    }
    /**
     * To choose file
     * @return
     */
    public static File selectFile() {
        JFileChooser f = new JFileChooser();
        int returnVal = -1;
        while(returnVal != JFileChooser.APPROVE_OPTION) {
            returnVal = f.showOpenDialog(null);
        }
        return f.getSelectedFile();
    }
}